<?php
/* ******************************
 *	Recipie Production List aJax actions
 * ******************************
 */
 
include('../../../includes/classes/core.php');
$action	= $_REQUEST['act'];
$error	= '';
$data	= '';

switch ($action) {
    case 'get_add_page':
		$page		= GetPage();
		$data		= array('page'	=> $page);
		
        break;
    case 'get_edit_page':
	    $goods_id	= $_REQUEST['id'];
	    $str        = $goods_id[0];
		if($str == 'O'){
			$g_id = substr($goods_id, 1); ;
			$page		= GetOverheadPage(GetOverheadGoods($g_id),$g_id);
		}else{
			$page = GetPage(GetGoods($goods_id));
		}
		
	    
        $data = array('page'	=> $page);
        break;
    case 'get_list':
	    $count		= $_REQUEST['count'];
	    $hidden		= $_REQUEST['hidden'];
	    $doc_numb   = $_REQUEST['doc_numb'];

	    $check      =GetDocType($doc_numb);
	    

	    
	    mysql_query("SET @i = 0;");
	    if($check){

		    
		    $rResult = mysql_query("SELECT DISTINCT CONCAT('O', `overhead_detail`.`id`) AS `id`,
        					@i := @i + 1 AS `iterator`,
        					`overhead_detail`.`name` AS `goods_name`,
        					`production`.`name`,
        					`overhead_correction_detail`.`old_quantity`,
        					`overhead_correction_detail`.`old_cost`,
        					`overhead_correction_detail`.`old_quantity` * `overhead_correction_detail`.`old_cost`
        	
        					FROM `overhead_correction_detail`
							 JOIN overhead_detail ON overhead_detail.id = overhead_correction_detail.overhead_detail_id
							 JOIN overhead ON overhead.id = overhead_detail.overhead_id
        					 JOIN `production_identity` ON `overhead_detail`.`name` = `production_identity`.`rs_name`
        					 JOIN `production` ON `production_identity`.`production_id` = `production`.`id`
        					 JOIN `overhead_identity` ON `overhead_detail`.`overhead_id` = `overhead_identity`.`overhead_id` && `production_identity`.`id` = `overhead_identity`.`identity_id` && `overhead_detail`.`id` = `overhead_identity`.`overhead_detail_id`
        					WHERE overhead.waybill_number = '$doc_numb' AND  overhead.waybill_number != ''");
	    }else{

			    	$rResult = mysql_query("SELECT  `overhead_detail`.`id` AS `id`,
												     @i := @i + 1 AS `iterator`,
										`overhead_detail`.`name` AS `goods_name`,
										`overhead_detail`.`name` AS `goods_name`,
										`overhead_correction_detail`.`old_quantity`,
										`overhead_correction_detail`.`old_cost`,
										`overhead_correction_detail`.`old_quantity`*`overhead_correction_detail`.`old_cost`
						
										FROM `overhead_correction_detail`
										 JOIN overhead_detail ON overhead_detail.id = overhead_correction_detail.overhead_detail_id
										 JOIN overhead ON overhead.id = overhead_detail.overhead_id
			    			WHERE overhead.waybill_number = '$doc_numb' AND  overhead.waybill_number != ''");
	
	    }
	    
		$data = array(
			"aaData"	=> array()
		);

		
		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				/* General output */
				$row[] = $aRow[$i];
				if($i == ($count - 1)){
					$row[] = '<input type="checkbox" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" />';
				}
			}
			$data['aaData'][] = $row;
		}
		
        break;
    case 'get_list_after':
        	$count		= $_REQUEST['count'];
        	$hidden		= $_REQUEST['hidden'];
        	$doc_numb   = $_REQUEST['doc_numb'];
        
        	$check      =GetDocType($doc_numb);
        	 
        
        	 
        	mysql_query("SET @i = 0;");
        	if($check){
        
        
        		$rResult = mysql_query("SELECT DISTINCT CONCAT('O', `overhead_detail`.`id`) AS `id`,
				        					@i := @i + 1 AS `iterator`,
				        					`overhead_detail`.`name` AS `goods_name`,
				        					`production`.`name`,
				        					`overhead_correction_detail`.`new_quantity`,
				        					`overhead_correction_detail`.`new_cost`,
				        					`overhead_correction_detail`.`new_quantity` * `overhead_correction_detail`.`new_cost`
				        	
				        					FROM `overhead_correction_detail`
											 JOIN overhead_detail ON overhead_detail.id = overhead_correction_detail.overhead_detail_id
											 JOIN overhead ON overhead.id = overhead_detail.overhead_id
				        					 JOIN `production_identity` ON `overhead_detail`.`name` = `production_identity`.`rs_name`
				        					 JOIN `production` ON `production_identity`.`production_id` = `production`.`id`
				        					 JOIN `overhead_identity` ON `overhead_detail`.`overhead_id` = `overhead_identity`.`overhead_id` && `production_identity`.`id` = `overhead_identity`.`identity_id` && `overhead_detail`.`id` = `overhead_identity`.`overhead_detail_id`
				        					WHERE overhead.waybill_number = '$doc_numb' AND  overhead.waybill_number != ''
        				 		 ");
        	}else{
        
        		$rResult = mysql_query("SELECT  `overhead_detail`.`id` AS `id`,
        				@i := @i + 1 AS `iterator`,
        				`overhead_detail`.`name` AS `goods_name`,
        				`overhead_detail`.`name` AS `goods_name`,
        				`overhead_correction_detail`.`new_quantity`,
        				`overhead_correction_detail`.`new_cost`,
        				`overhead_correction_detail`.`new_quantity`*`overhead_correction_detail`.`new_cost`
        
										FROM `overhead_correction_detail`
										 JOIN overhead_detail ON overhead_detail.id = overhead_correction_detail.overhead_detail_id
										 JOIN overhead ON overhead.id = overhead_detail.overhead_id
			    			WHERE overhead.waybill_number = '$doc_numb' AND  overhead.waybill_number != ''");
        
        	}
        	 
        	$data = array(
        			"aaData"	=> array()
        	);
        
        
        	while ( $aRow = mysql_fetch_array( $rResult ) )
        	{
        		$row = array();
        		for ( $i = 0 ; $i < $count ; $i++ )
        		{
        			/* General output */
        			$row[] = $aRow[$i];
        			if($i == ($count - 1)){
        				$row[] = '<input type="checkbox" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" />';
        			}
        		}
        		$data['aaData'][] = $row;
        	}
        
        	break;
      case 'get_list_diff':
        		$count		= $_REQUEST['count'];
        		$hidden		= $_REQUEST['hidden'];
        		$doc_numb   = $_REQUEST['doc_numb'];
        	
        		$check      =GetDocType($doc_numb);
        	
        	
        	
        		mysql_query("SET @i = 0;");
        		if($check){
        	
        	
        			$rResult = mysql_query("SELECT DISTINCT CONCAT('O', `overhead_detail`.`id`) AS `id`,
        					@i := @i + 1 AS `iterator`,
        					`overhead_detail`.`name` AS `goods_name`,
        					`production`.`name`,
        					`overhead_correction_detail`.`new_quantity` - `overhead_correction_detail`.`old_quantity`,
        					`overhead_correction_detail`.`new_cost` - `overhead_correction_detail`.`old_cost`,
        					(`overhead_correction_detail`.`new_quantity` - `overhead_correction_detail`.`old_quantity`) * (`overhead_correction_detail`.`new_cost` - `overhead_correction_detail`.`old_cost`)
        					FROM `overhead_correction_detail`
							 JOIN overhead_detail ON overhead_detail.id = overhead_correction_detail.overhead_detail_id
							 JOIN overhead ON overhead.id = overhead_detail.overhead_id
        					 JOIN `production_identity` ON `overhead_detail`.`name` = `production_identity`.`rs_name`
        					 JOIN `production` ON `production_identity`.`production_id` = `production`.`id`
        					 JOIN `overhead_identity` ON `overhead_detail`.`overhead_id` = `overhead_identity`.`overhead_id` && `production_identity`.`id` = `overhead_identity`.`identity_id` && `overhead_detail`.`id` = `overhead_identity`.`overhead_detail_id`
        					WHERE overhead.waybill_number = '$doc_numb' AND  overhead.waybill_number != ''

/*
SELECT  `overhead_detail`.`id` AS `id`,
        					@i := @i + 1 AS `iterator`,
        					`overhead_detail`.`name` AS `goods_name`,
        					`overhead_detail`.`name` AS `goods_name`,
        					`overhead_correction_detail`.`new_quantity` - `overhead_correction_detail`.`old_quantity`,
        					`overhead_correction_detail`.`new_cost` - `overhead_correction_detail`.`old_cost`,
        					(`overhead_correction_detail`.`new_quantity` - `overhead_correction_detail`.`old_quantity`) * (`overhead_correction_detail`.`new_cost` - `overhead_correction_detail`.`old_cost`)
        	
										FROM `overhead_correction_detail`
										 JOIN overhead_detail ON overhead_detail.id = overhead_correction_detail.overhead_detail_id
										 JOIN overhead ON overhead.id = overhead_detail.overhead_id
			    			WHERE overhead.waybill_number = '$doc_numb' AND  overhead.waybill_number != ''
*/
        					");
        		}else{
        	
        			$rResult = mysql_query("SELECT  `overhead_detail`.`id` AS `id`,
        					@i := @i + 1 AS `iterator`,
        					`overhead_detail`.`name` AS `goods_name`,
        					`overhead_detail`.`name` AS `goods_name`,
        					`overhead_correction_detail`.`new_quantity` - `overhead_correction_detail`.`old_quantity`,
        					`overhead_correction_detail`.`new_cost` - `overhead_correction_detail`.`old_cost`,
        					(`overhead_correction_detail`.`new_quantity` - `overhead_correction_detail`.`old_quantity`) * (`overhead_correction_detail`.`new_cost` - `overhead_correction_detail`.`old_cost`)
        	
										FROM `overhead_correction_detail`
										 JOIN overhead_detail ON overhead_detail.id = overhead_correction_detail.overhead_detail_id
										 JOIN overhead ON overhead.id = overhead_detail.overhead_id
			    			WHERE overhead.waybill_number = '$doc_numb' AND  overhead.waybill_number != ''");
        	
        		}
        	
        		$data = array(
        				"aaData"	=> array()
        		);
        	
        	
        		while ( $aRow = mysql_fetch_array( $rResult ) )
        		{
        			$row = array();
        			for ( $i = 0 ; $i < $count ; $i++ )
        			{
        				/* General output */
        				$row[] = $aRow[$i];
        				if($i == ($count - 1)){
        					$row[] = '<input type="checkbox" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" />';
        				}
        			}
        			$data['aaData'][] = $row;
        		}
        	
        		break;
    case 'save_prod':
		$comp_id		= $_REQUEST['id'];
		$local_id		= $_REQUEST['l_id'];
		
    	$prod_name		= $_REQUEST['tn'];
		$quantity 		= $_REQUEST['tq'];
		$source_object	= $_REQUEST['so'];
		
		$prod_id		= GetProductID($prod_name);
		$quantity_c		= CheckQuantity($prod_id, $source_object, $quantity);
		
		if($quantity_c){			
			if($comp_id == ''){
				if(!CheckProductionExist($local_id, $prod_id)){
					AddProduction($local_id, $prod_id, $quantity);
				}else{
					$error = '"' . $prod_name . '" უკვე არის სიაში!';
				}
			}else{
				SaveProduction($comp_id, $prod_id, $quantity);
			}
		}else{
			$error = 'არასაკმარისი პროდუქტი მარაგში!';
		}
		
        break;
    case 'act_info' :
    	$act_id = $_REQUEST['act_id'];
    	
    	$page		= GetActPage(GetActInfo($act_id));
    	$data		= array('page'	=> $page);
    	
    	break;
    case 'disable':
		$prod_id = $_REQUEST['id'];
		DeleteProduction($prod_id);
		
        break;
    case 'get_goods':
        $barcode 		= $_REQUEST['bc'];
        $name 			= $_REQUEST['nm'];
        $res 			= GetGoodsDetails($barcode, $name);
        if(!$res){
        	$error = 'საქონელი ვერ მოიძებნა!';
        }else{
        	$data = array('code' => $res['code'],
        			'name' => $res['name'],
        			'measure' => $res['unit'],
        			'price' => $res['price']);
        }
        
        	break;
    default:
       $error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);


/* ******************************
 *	Recipie Production List Functions
 * ******************************
 */


function GetGoodsDetails($code, $name)
{

	if($code != '' && $name != ''){
		$res = mysql_query("	SELECT	`PROD`.`id`,
				`PROD`.`code`,
				`PROD`.`name`,
				`PROD`.`unit`
				FROM `production` AS `PROD`
				WHERE `PROD`.`code` = '$code' && `PROD`.`name` = '$name'");
	}else if($code != ''){
		$res = mysql_query("	SELECT	`PROD`.`id`,
				`PROD`.`code`,
				`PROD`.`name`,
				`PROD`.`unit`
				FROM `production` AS `PROD`
				WHERE `PROD`.`code` = '$code'");
	}else if($name != ''){
		$res = mysql_query("	SELECT	`id`,
				`code`,
				`name`,
				`unit`
				FROM	`production`
				WHERE	`name` = '$name'");
	}

	if (mysql_num_rows($res) == 0){
		return false;
	}
	$row = mysql_fetch_assoc($res);
	return $row;
}

function GetDocType($doc_numb){
	$check = false;
	$res = mysql_fetch_assoc(mysql_query("SELECT overhead.stream AS `stream`
											FROM overhead
											WHERE overhead.waybill_number = '$doc_numb'"));
	
	if($res[stream] == 1){
		$check = true;
	}
	
	return $check;
}

function AddProduction($local_id, $prod_id, $quantity)
{
	mysql_query("INSERT INTO `transfer_detail`
					(`transfer_id`, `production_id`, `quantity`) 
				 VALUES
					($local_id, $prod_id, $quantity)");
}

function SaveProduction($comp_id, $prod_id, $quantity) 
{
	mysql_query("UPDATE
	    			`transfer_detail`
				 SET
					`production_id`	= $prod_id,
					`quantity`		= $quantity
				 WHERE
					`id` = $comp_id");
}

function CheckQuantity($prod_id, $obj_id, $req_quantity)
{
	$res = mysql_query("SELECT SUM(`quantity` - `sold`) AS `quantity`
						FROM `reserve`
						WHERE `production_id` = $prod_id && `object_id` = $obj_id");
	$row = mysql_fetch_assoc($res);
	if($row['quantity'] >= $req_quantity){
		return true;
	}
	return false;
}

function DeleteProduction($prod_id)
{
    mysql_query("DELETE 
				 FROM
	    			`transfer_detail`
				 WHERE
					`id` = $prod_id");
}

function CheckProductionExist($local_id, $prod_id) 
{
    $res = mysql_fetch_assoc(mysql_query("SELECT `id`
										  FROM `transfer_detail`
										  WHERE `transfer_id` = $local_id && `production_id` = $prod_id"));
	if($res['id'] != ''){
		return true;
	}
	return false;
}

function GetProductID($prod_name) 
{
	$prod_name = htmlspecialchars($prod_name, ENT_QUOTES);
    $res = mysql_fetch_assoc(mysql_query("SELECT 	`id`
										  FROM 		`production`
										  WHERE 	`name` = '$prod_name'"));
	return $res['id'];
}

function GetProduction($prod_id)
{	
    $res = mysql_fetch_assoc(mysql_query("SELECT `TDET`.`id`,
												 `PROD`.`name`,
												 `TDET`.`quantity`
										  FROM `transfer_detail` AS `TDET` LEFT JOIN `production` AS `PROD`
											ON `TDET`.`production_id` = `PROD`.`id`
										  WHERE `TDET`.`id` = $prod_id"));
	return $res;
}

function GetGoods($goods_id) 
{
    $res = mysql_fetch_assoc(mysql_query("SELECT `id`,
    											 `bar_code`,
											     `name`,
											     `unit_id`,
											   	 `quantity`,
											   	 `price`,
											   	 `amount`
									      FROM `overhead_detail`
									      WHERE `id` = $goods_id"));
	return $res;
}

function GetUnit($point)
{
	$data = '';
	$req = mysql_query("SELECT `id`, `name`
							FROM `production_unit`");

	if($point == ''){
		$data = '<option value="0" selected="selected"></option>';
	}

	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $point){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}

function GetObject($object_id)
{
	$data = '';
	$req = mysql_query("SELECT	`id`, `name`
						FROM	`object`
						WHERE	`object`.`type` != 1 && `object`.`type` != 5 && `object`.`type` != 7 && `object`.`actived` = 1 && `object`.`type` = 2"); // <<<<---------------------Type!!!

	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $object_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}
	return $data;
}

function GetActInfo($act_id)
{
	$res = mysql_fetch_assoc(mysql_query("SELECT `id`,
			`waybill_number`,
			DATE(`activate_date`) AS `activate_date`,
			`seller_tin`,
			`seller_name`,
			`status`,
			`full_amount`,
			`stream`
			FROM `overhead`
			WHERE waybill_number = '$act_id'"));
	return $res;
}

function GetActPage($res){
   $doctype = $res[stream];
   $type = ($res[stream]==1) ? "ზედნადები" : "შეძენის აქტი";
   
   $data = '<fieldset>
	    	<legend>ძირითადი ინფორმაცია</legend>

	    	<table width="100%" class="dialog-form-table">
				<tr>
   					<td style="width: 200px;"><label for="doc_type">დოკუმენტის ტიპი</label></td>
					<td style="width: 200px;"><label for="d_number">პარტნიორი</label></td>
					<td style="width: 200px;"><label for="operation_data">ოპერაციის თარიღი</label></td>
					<td style="width: 200px;"><label for="object" class="object">საწყობი</label></td>
				</tr>
				<tr>
   					<td>
						<input type="text" id="doc_type" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="'.$type.'" disabled/>
					</td>
					<td>
						<input type="text" id="d_number" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="'.$res[seller_name].'" disabled/>
					</td>
					<td>
						<input type="text" id="operation_data" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="'.$res[activate_date].'" disabled/>
					</td>
					<td>
						<select id="object" class="idls object" disabled>'.GetObject($res[id]).'</select>
					</td>
				</tr>
			</table>
        </fieldset>';
   
   return  $data;
   
}

function GetPage($res = '')
{
	$data = '
	<div id="dialog-form">
	    <fieldset>
	    	<legend>ძირითადი ინფორმაცია</legend>

	    	<table class="dialog-form-table">
				<tr>
					<td style="width: 170px;"><label for="goods_code">საქონლის კოდი</label></td>
					<td>
						<input type="text" id="goods_code" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['bar_code'] . '" />
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="goods_name">საქონლის დასახელება</label></td>
					<td>
						<div class="seoy-row" id="goods_name_seoy">
							<input type="text" id="goods_name" class="idle seoy-address" onblur="this.className=\'idle seoy-address\'" onfocus="this.className=\'activeField seoy-address\'" value="' . $res['name'] . '" />
							<button id="goods_name_btn" class="combobox">goods_name</button>
						</div>
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="goods_measure">ზომის ერთეული</label></td>
					<td>
						<select id="goods_measure" class="idls num" disabled="disabled">' . GetUnit($res[unit_id]) . '</select>
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="goods_quantity">რაოდენობა</label></td>
					<td>
						<input type="text" id="goods_quantity" class="idle num" onblur="this.className=\'idle num\'" onfocus="this.className=\'activeField num\'" value="' . $res['quantity'] . '" />
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="goods_unit_price">ერთეულის ფასი</label></td>
					<td>
						<input type="text" id="goods_unit_price" class="idle num" onblur="this.className=\'idle num\'" onfocus="this.className=\'activeField num\'" value="' . $res['price'] . '" />
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="goods_price">საქონლის ფასი</label></td>
					<td>
						<input type="text" id="goods_price" class="idle num" onblur="this.className=\'idle num\'" onfocus="this.className=\'activeField num\'" value="' . $res['amount'] . '" disabled/>
					</td>
				</tr>
			</table>
			<!-- ID -->
			<input type="hidden" id="goods_id" value="' . $res['id'] . '" />
        </fieldset>
    </div>
    ';
	return $data;
}


function GetVat($point)
{
	$data = '';
	$req = mysql_query("SELECT `id`, `name`
							FROM `overhead_vat`");

	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $point){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}

function GetOverheadGoods($goods_id)
{
	$res = mysql_query("	SELECT  	`OD`.`id`,
			`OI`.`id` AS `identity_id`,
			`OD`.`bar_code`,
			`OD`.`name`,
			`PR`.`name` AS `prod_name`,
			`OD`.`unit_id`,
			`PU`.`name` AS `unit`,
			`OD`.`quantity`,
			`OD`.`price`,
			`OD`.`amount`,
			`OD`.`vat_type`,
			`OI`.`quantity` AS `i_quantity`,
			`OI`.`price` AS `i_price`,
			`OI`.`overhead_detail_id` AS `overhead_detail_id`
			FROM		`overhead_detail` AS `OD`
			LEFT JOIN   `overhead` ON `overhead`.`id` = `OD`.`overhead_id`
			LEFT JOIN 	`production_identity` AS `PI`ON `OD`.`name` = `PI`.`rs_name`
			LEFT JOIN 	`production` AS `PR` ON `PI`.`production_id` = `PR`.`id`
			LEFT JOIN 	`production_unit` AS PU ON PR.unit =  PU.id
			LEFT JOIN 	`overhead_identity` AS `OI` ON `OD`.`id` = `OI`.`overhead_detail_id` && `PI`.`id` = `OI`.`identity_id`
			WHERE 		`OD`.`id` = $goods_id");
	return mysql_fetch_assoc($res);
}

function GetOverheadPage($res, $goods_id)
{
	$data = '
	<div id="dialog-form">
	    <fieldset>
	    	<legend>ძირითადი ინფორმაცია</legend>
	    	
	    	<table class="dialog-form-table">
				<tr>
					<td style="width: 170px;"><label for="goods_code">საქონლის კოდი</label></td>
					<td>
						<input type="text" id="goods_code" class="idle" disabled="disabled" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['bar_code'] . '" />
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="goods_name">დასახელება ზედნადებით</label></td>
					<td>
						<div class="seoy-row" id="goods_name_seoy">
							<input type="text" id="goods_name" class="idle seoy-address" disabled="disabled" onblur="this.className=\'idle seoy-address\'" onfocus="this.className=\'activeField seoy-address\'" value="' . $res['name'] . '" />
							<button id="goods_name_btn" class="combobox" disabled="disabled">goods_name</button>
						</div>
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="goods_measure">ზომის ერთეული ზენადებით</label></td>
					<td>
						<select id="goods_measure" class="idls num" disabled="disabled">' . GetUnit($res[unit_id]) . '</select>
					</td>
				</tr>					
				<tr>
					<td style="width: 170px;"><label for="goods_local_name">საქონლის დასახელება</label></td>
					<td>
						<div class="seoy-row" id="goods_local_name_seoy">
							<input type="text" id="goods_local_name" class="idle seoy-address" onblur="this.className=\'idle seoy-address\'" onfocus="this.className=\'activeField seoy-address\'" value="' . $res['prod_name'] . '"/>
							<button id="goods_local_name_btn" class="combobox">goods_local_name</button>
						</div>
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="goods_local_unit">ზომის ერთეული</label></td>
					<td>
						<input type="text" id="goods_local_unit" class="idle num" onblur="this.className=\'idle seoy-address\'" onfocus="this.className=\'activeField seoy-address\'" value="' . $res['unit'] . '" disabled="disabled"/>
					</td>
				</tr>
				';
	
	if(empty($res['i_quantity']) && empty($res['i_price'])){
		$data .= '
				<tr>
					<td style="width: 170px;"><label for="goods_local_quantity">რაოდენობა</label></td>
					<td>
						<input type="text" id="goods_local_quantity" class="idle num" onblur="this.className=\'idle num\'" onfocus="this.className=\'activeField num\'" value="' . $res['quantity'] . '" />
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="goods_local_unit_price">ერთეულის ფასი</label></td>
					<td>
						<input type="text" id="goods_local_unit_price" class="idle num"  onblur="this.className=\'idle num\'" onfocus="this.className=\'activeField num\'" value="' . $res['price'] . '" />
					</td>
				</tr>';
	}else{
		$data .= '
				<tr>
					<td style="width: 170px;"><label for="goods_local_quantity">რაოდენობა</label></td>
					<td>
						<input type="text" id="goods_quantity" class="idle num" onblur="this.className=\'idle num\'" onfocus="this.className=\'activeField num\'" value="' . $res['i_quantity'] . '" />
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="goods_unit_price">ერთეულის ფასი</label></td>
					<td>
						<input type="text" id="goods_unit_price" class="idle num"  onblur="this.className=\'idle num\'" onfocus="this.className=\'activeField num\'" value="' . $res['i_price'] . '" />
					</td>
				</tr>';
	}
		
	$data .= '
				<tr>
					<td style="width: 170px;"><label for="goods_price">საქონლის ფასი</label></td>
					<td>
						<input type="text" id="goods_price" class="idle num" disabled="disabled" onblur="this.className=\'idle num\'" onfocus="this.className=\'activeField num\'" value="' . $res['amount'] . '" />
					</td>
				</tr>
				<tr>
					<td style="width: 170px;"><label for="goods_vat">დაბეგვრა</label></td>
					<td>
						<select id="goods_vat" class="idls medium" disabled="disabled">' . GetVat($res[vat_type]) . '</select>
					</td>
				</tr>
			</table>
			<!-- ID -->
			<input type="hidden" id="identity_id" value="' . $res['identity_id'] . '" />
			<input type="hidden" id="goods_id" value="' . 'O'. $goods_id . '" />
			<input type="hidden" id="overhead_detail_id_local" value="' . $res['overhead_detail_id'] . '" />
        </fieldset>
    </div>
    ';
	return $data;
}



?>